﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

/* ==============================================================================
   * 功能描述：WindowBtnCommand  
   * 创 建 者：Administrator
   * 创建日期：2022-04-11 14:27:45
   * ==============================================================================*/
namespace Zy.WPF.CtrLib.Ctrs
{
    /// <summary>
    /// 自定义窗口的按钮动作
    /// </summary>
    public class ZyCommand : ICommand
    {
        /// <summary>
        /// 自定义窗口的按钮动作
        /// </summary>
        /// <param name="action">动作</param>
        public ZyCommand(Action<object> action) : this(action, null) { }

        public ZyCommand(Action<object> action, Func<object, bool> _canExecute)
        {
            DoAction = action ?? throw new ArgumentNullException(nameof(action));
            this.canExecute = _canExecute ?? (x => true);
        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        private readonly Func<object, bool> canExecute;
        /// <summary>
        /// 执行的方法
        /// </summary>
        public Action<object> DoAction { get; set; }
        /// <summary>
        /// 检查是否可以执行该方法
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public bool CanExecute(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="parameter"></param>
        public void Execute(object parameter)
        {
            if (DoAction != null)
            {
                DoAction(parameter);
            }
        }
    }
}
